﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.Security;

namespace Fitment.User
{
    public partial class testhouseInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string userName = Session["userName"].ToString();   //用于获取用户登录身份
            this.user.Text = userName;

            //如果Session为空,则转到登陆页.
            if (Session["User_ID"] == null)
            {
                Response.Redirect("../logon.aspx");
            }
            if (!IsPostBack)
            {
                Session["House_ID"] = null;
            }
            //如果已经设定了各个房间个数，则动态生成控件输入门窗数
            if (Session["House_ID"] != null)
            {
                loadData();
            }

        }

        protected void next_Click1(object sender, ImageClickEventArgs e)
        {
            //更新各个门窗信息
            int Ordinal = 1;
            DS data1 = new DS();
            string ID = "ID_" + Ordinal.ToString();
            string RT = "RT_" + Ordinal.ToString();
            string DN = "DN_" + Ordinal.ToString();
            string WN = "WN_" + Ordinal.ToString();
            while (FindControl(ID) != null)
            {
                Label RoomID = (Label)FindControl(ID);
                TextBox DoorNum = (TextBox)FindControl(DN);
                TextBox WindNum = (TextBox)FindControl(WN);
                string IDoor1 = "INSERT INTO DoorInfo (Room_ID,DoorHeight,DoorLength) VALUES ('" + RoomID.Text + "', '" + 0 + "','" + 0 + "');";
                string IWindow1 = "INSERT INTO WindowInfo (Room_ID,WindHeight,WindLength) VALUES ('" + RoomID.Text + "', '" + 0 + "','" + 0 + "');";
                for (int i = 0; i < Convert.ToInt32(DoorNum.Text); i++)
                {
                    data1.ExcuteSql(IDoor1);
                }
                for (int i = 0; i < Convert.ToInt32(WindNum.Text); i++)
                {
                    data1.ExcuteSql(IWindow1);
                }
                Ordinal++;
                ID = "ID_" + Ordinal.ToString();
                RT = "RT_" + Ordinal.ToString();
                DN = "DN_" + Ordinal.ToString();
                WN = "WN_" + Ordinal.ToString();
            }
            Session["flag"] = "true";
            Response.Redirect("roomMatInfo.aspx");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Session["House_ID"] != null)
            {
                Button1.Visible = false;
                lbNext.Visible = true;
                next.Visible = true;
            }
            if (Session["House_ID"] == null)
            {
                //新建数据操作对象
                DS data = new DS();

                //获取输入数据
                int UserID = Convert.ToInt32(Session["User_ID"].ToString());
                int HouseID;
                int d = Convert.ToInt32(tb_doorc.Text.ToString().Trim());
                int w = Convert.ToInt32(tb_windc.Text.ToString().Trim());
                int r = Convert.ToInt32(tb_roomc.Text.ToString().Trim());

                int bedroomcount = Convert.ToInt32(TextBox1.Text.ToString().Trim());
                int livingroomcount = Convert.ToInt32(TextBox2.Text.ToString().Trim());
                int kitchencount = Convert.ToInt32(TextBox3.Text.ToString().Trim());
                int bathroomcount = Convert.ToInt32(TextBox4.Text.ToString().Trim());
                int bookroomcount = Convert.ToInt32(TextBox5.Text.ToString().Trim());

                //总数不等给出提示
                if (r != (bedroomcount + livingroomcount + kitchencount + bathroomcount + bookroomcount))
                {
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "提示", "<script>alert('各房间数之和与总数不相等，请检查！');</script>");
                    return;
                }

                //读取配置文件连接信息
                string strConnection = ConfigurationManager.ConnectionStrings["connection"].ToString();

                //把信息插入房子表中
                string kk = "INSERT INTO HouseInfo (User_ID,DoorCount, WindCount,RoomCount) VALUES ('" + UserID + "','" + d + "', '" + w + "','" + r + "');";
                data.ExcuteSql(kk);

                //得到登录用户的House_ID
                string GetHouseID = "SELECT MAX(House_ID) FROM HouseInfo WHERE User_ID='" + UserID + "'";
                using (SqlConnection connection = new SqlConnection(strConnection))
                {
                    SqlCommand command = new SqlCommand(GetHouseID, connection);
                    connection.Open();
                    SqlDataReader Reader = command.ExecuteReader();
                    Reader.Read();
                    HouseID = Convert.ToInt32(Reader[0]);
                    Reader.Close();
                    connection.Close();
                }
                Session["House_ID"] = HouseID;

                //添加卧室类型房间
                string k1 = "INSERT INTO RoomInfo (House_ID,RoomType, RoomHeight,RoomLength,RoomWidth) VALUES ('" + HouseID + "','" + "卧室" + "','" + 0 + "', '" + 0 + "','" + 0 + "');";
                for (int i = 0; i < bedroomcount; i++)
                {
                    data.ExcuteSql(k1);
                }

                //添加客厅类型房间
                string k2 = "INSERT INTO RoomInfo (House_ID,RoomType, RoomHeight,RoomLength,RoomWidth) VALUES ('" + HouseID + "','" + "客厅" + "','" + 0 + "', '" + 0 + "','" + 0 + "');";
                for (int i = 0; i < livingroomcount; i++)
                {
                    data.ExcuteSql(k2);
                }

                //添加厨房类型房间
                string k3 = "INSERT INTO RoomInfo (House_ID,RoomType, RoomHeight,RoomLength,RoomWidth) VALUES ('" + HouseID + "','" + "厨房" + "','" + 0 + "', '" + 0 + "','" + 0 + "');";
                for (int i = 0; i < kitchencount; i++)
                {
                    data.ExcuteSql(k3);
                }

                //添加卫生间类型房间
                string k4 = "INSERT INTO RoomInfo (House_ID,RoomType, RoomHeight,RoomLength,RoomWidth) VALUES ('" + HouseID + "','" + "卫生间" + "','" + 0 + "', '" + 0 + "','" + 0 + "');";
                for (int i = 0; i < bathroomcount; i++)
                {
                    data.ExcuteSql(k4);
                }

                //添加书房类型房间
                string k5 = "INSERT INTO RoomInfo (House_ID,RoomType, RoomHeight,RoomLength,RoomWidth) VALUES ('" + HouseID + "','" + "书房" + "','" + 0 + "', '" + 0 + "','" + 0 + "');";
                for (int i = 0; i < bookroomcount; i++)
                {
                    data.ExcuteSql(k5);
                }
                loadData();
                Button1.Visible = false;
                lbNext.Visible = true;
                next.Visible = true;
            }
        }

        protected void LogoutLinkButton_Click1(object sender, EventArgs e)
        {
            FormsAuthentication.SignOut();  //登出页面并释放所有session
            Session.Abandon();
            Response.Redirect("../logon.aspx");
        }
        protected void loadData()
        {
            string strConnection = ConfigurationManager.ConnectionStrings["connection"].ToString();

            int UserID = Convert.ToInt32(Session["User_ID"].ToString());

            // 房间序数
            int Order = 0;

            //依次操作属于该房子的房间
            string GetRoomInfo = "SELECT * FROM RoomInfo WHERE House_ID in (SELECT MAX(House_ID) FROM HouseInfo WHERE User_ID='" + UserID + "')";
            using (SqlConnection connection = new SqlConnection(strConnection))
            {
                SqlCommand command = new SqlCommand(GetRoomInfo, connection);
                connection.Open();
                SqlDataReader Reader = command.ExecuteReader();
                while (Reader.Read())
                {
                    Order++;
                    //新建容器并给ID赋值
                    Panel P = new Panel();
                    P.ID = "P_" + Order.ToString();
                    Panel1.Controls.Add(P);

                    //新建控件并给ID赋值
                    Label RoomID = new Label();
                    RoomID.ID = "ID_" + Order.ToString();
                    RoomID.Text = Reader[0].ToString();
                    Label RoomType = new Label();
                    RoomType.ID = "RT_" + Order.ToString();
                    RoomType.Text = Reader[2].ToString();
                    RoomType.Width = 80;
                    Label L1 = new Label();
                    L1.Text = "门数：";
                    TextBox DoorN = new TextBox();
                    DoorN.ID = "DN_" + Order.ToString();
                    DoorN.Text = "0";
                    Label L2 = new Label();
                    L2.Text = "窗数：";
                    TextBox WindN = new TextBox();
                    WindN.ID = "WN_" + Order.ToString();
                    WindN.Text = "0";

                    //在容器中添加控件
                    P.Controls.Add(RoomID);
                    P.Controls.Add(RoomType);
                    P.Controls.Add(L1);
                    P.Controls.Add(DoorN);
                    P.Controls.Add(L2);
                    P.Controls.Add(WindN);
                }
                Reader.Close();
                connection.Close();
            }
        }
    }
}